package com.x.reader.service;

import com.x.framework.Base;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;
import com.x.reader.dao.AudioDao;
import com.x.reader.dao.UserDao;
import com.x.reader.model.Audio;
import com.x.reader.model.AudioChapter;
import com.x.reader.model.User;
import org.directwebremoting.io.FileTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AudioService extends BaseService {

    @Autowired
    private AudioDao audioDao;

    public Audio insertAudio(Audio audio, FileTransfer audioCoverFile) {
        try {
            String audioId = this.getUid(32);
            String audioCover = audioCoverFile.getFilename();
            audioCover = audioId + audioCover.substring(audioCover.lastIndexOf(".")).toLowerCase();
            audio.setAudioId(audioId);
            audio.setAudioCover(audioCover);
            audio.setAudioCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            audio = this.insert(audio);
            this.uploadFile(audioCoverFile, "/upload/audio/cover", audioCover);
            return audio;
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void updateAudio(Audio audio, FileTransfer audioCoverFile) {
        try {
            String audioCover = null;
            if (Base.isNotNull(audioCoverFile)) {
                String audioId = audio.getAudioId();
                audioCover = audioCoverFile.getFilename();
                audioCover = audioId + audioCover.substring(audioCover.lastIndexOf(".")).toLowerCase();
                audio.setAudioCover(audioCover);
            }
            audio.setAudioCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            this.update(audio);
            if (Base.isNotNull(audioCoverFile)) {
                this.deleteFile("/upload/audio/cover", audioCover);
                this.uploadFile(audioCoverFile, "/upload/audio/cover", audioCover);
            }
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void deleteAudio(Audio audio) {
        try {
            if (Base.isNotNull(audio.getAudioCover())) {
                this.deleteFile("/upload/audio/cover", audio.getAudioCover());
            }
            Audio a = new Audio();
            a.setAudioId(audio.getAudioId());
            this.delete(a);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<Audio> selectAudioList(Audio audio) {
        try {
            return audioDao.selectAudioList(audio);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public int selectAudioNum(Audio audio) {
        try {
            return audioDao.selectAudioNum(audio);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public AudioChapter insertAudioChapter(AudioChapter audioChapter, FileTransfer audioChapterFileFile) {
        try {
            String audioChapterId = this.getUid(32);
            String audioChapterFile = audioChapterFileFile.getFilename();
            audioChapterFile = audioChapterId + audioChapterFile.substring(audioChapterFile.lastIndexOf(".")).toLowerCase();
            audioChapter.setAudioChapterId(audioChapterId);
            audioChapter.setAudioChapterFile(audioChapterFile);
            audioChapter.setAudioChapterCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            audioChapter = this.insert(audioChapter);
            this.uploadFile(audioChapterFileFile, "/upload/audio/file", audioChapterFile);
            return audioChapter;
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void updateAudioChapter(AudioChapter audioChapter, FileTransfer audioChapterFileFile) {
        try {
            String audioChapterFile = null;
            if (Base.isNotNull(audioChapterFileFile)) {
                String audioChapterId = audioChapter.getAudioChapterId();
                audioChapterFile = audioChapterFileFile.getFilename();
                audioChapterFile = audioChapterId + audioChapterFile.substring(audioChapterFile.lastIndexOf(".")).toLowerCase();
                audioChapter.setAudioChapterFile(audioChapterFile);
            }
            audioChapter.setAudioChapterCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            this.update(audioChapter);
            if (Base.isNotNull(audioChapterFile)) {
                this.deleteFile("/upload/audio/file", audioChapterFile);
                this.uploadFile(audioChapterFileFile, "/upload/audio/file", audioChapterFile);
            }
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public void deleteAudioChapter(AudioChapter audioChapter) {
        try {
            if (Base.isNotNull(audioChapter.getAudioChapterFile())) {
                this.deleteFile("/upload/audio/file", audioChapter.getAudioChapterFile());
            }
            AudioChapter a = new AudioChapter();
            a.setAudioChapterId(audioChapter.getAudioChapterId());
            this.delete(a);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }

    public List<AudioChapter> selectAudioChapterList(AudioChapter audioChapter) {
        try {
            return audioDao.selectAudioChapterList(audioChapter);
        } catch (Exception e) {
            throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
        }
    }
}
